package maldade.persistence.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import maldade.persistence.model.Example;

/**
 * DAO for the Example.
 * 
 * @author rnojiri
 */
public class ExampleDAO extends AbstractDAO<Example>
{
	private static final String ID_EXAMPLE = "idExample";
	private static final String NAME = "name";
	
	/**
	 * Returns all examples.
	 * 
	 * @return List<Example>
	 */
	@SuppressWarnings("unchecked")
	public List<Example> getAllExamples()
	{
		Criteria criteria = createCriteria();
		
		criteria.addOrder(Order.desc(ID_EXAMPLE));
		
		return criteria.list();
	}
	
	/**
	 * Returns examples by name. 
	 * 
	 * @param name
	 * @return List<Example>
	 */
	@SuppressWarnings("unchecked")
	public List<Example> getExamplesByName(String name)
	{
		Criteria criteria = createCriteria();
		
		criteria.add(Restrictions.ilike(NAME, name, MatchMode.ANYWHERE));
		
		return criteria.list();
	}
}
